package org.shan.hangtian.tp;

import com.mlnx.common.utils.MyLog;

import java.nio.ByteBuffer;

/**
 * Created by amanda.shan on 2020/10/2.
 */
public class HangtianPacket {

    private static final ByteBuffer buffer = ByteBuffer
            .allocate(120);

    MyLog log = MyLog.getLog(getClass());

    public static final byte[] Heads = {0x12, (byte) 0x34, 0x56}; // 数据包包头字节

    public static final byte END = 0x0D;

    private RunMode runMode;

    private byte command;    // 8-37 脑疲劳缓解模式、40-71 助眠模式

    public RunMode getRunMode() {
        return runMode;
    }

    public void setRunMode(RunMode runMode) {
        this.runMode = runMode;
    }

    public byte getCommand() {
        return command;
    }

    public void setCommand(byte command) {
        this.command = command;
    }

    /**
     * 更改音乐
     * @param runMode
     * @param command
     * @return
     */
    public static HangtianPacket changeMusic(RunMode runMode, byte command) {
        HangtianPacket hangtianPacket = new HangtianPacket();
        hangtianPacket.setCommand(command);
        hangtianPacket.setRunMode(runMode);

        return hangtianPacket;
    }

    public byte[] encode() {
        byte[] bytes = new byte[6];

        System.arraycopy(Heads, 0, bytes, 0, 3);
        bytes[3] = (byte) runMode.getCode();
        bytes[4] = command;
        bytes[5] = END;

        return bytes;
    }

    public void decode(byte[]  bytes){
        if(bytes[1] != 0x00 || bytes[2] != 0x0D){
            log.error("收到的数据包内容错误:{}", bytes);
        }else{
            runMode = RunMode.decode(bytes[0]);
        }
    }
}
